#! /usr/bin/env python

'''Unit tests of the dust_events_hdf5 module.

Mike Dorey  2011-04-04
'''


import h5py
import unittest
import dust_events_hdf5
from database_access import DustExperiment, DustEvent, DustType
import os


class DustEventsHdf5Tester(unittest.TestCase):
    '''Tests of the dust_events_hdf5 module.'''

    def setUp(self):
        '''Opens up the HDF5 file in memory.'''
        self.hdf5_file = h5py.File('dust_events_test.hdf5', mode='a', driver='core',
                 backing_store=False)
        self.user_name = 'test user'
        self.experiment_name = 'test experiment'
        self.remarks = 'this is a unittest'
        self.dust_type = DustType('NICKEL', '64')
        self.event_dict = {'timestamp': 1L, 'velocity': 2.0, 'charge': 3.0, 'mass': 4.0,
                      'terminalpotential': 5.0, 'lenspotential': 6.0, 'needlepotential': 7.0,
                      'pulsefreq': 8.0, 'pulsewidth': 9.0, 'pulseheight': 10.0, 
                      'selection': 1, 'waveforms': '1234567.hdf5'}
        self.dust_event = DustEvent(self.event_dict)
        self.dust_experiment = DustExperiment(self.user_name, self.experiment_name,
                             self.remarks, self.dust_type, [self.dust_event])
        self.assertEqual(self.dust_experiment.user_name, self.user_name)
        self.assertEqual(self.dust_experiment.experiment_name, self.experiment_name)
        self.assertEqual(self.dust_experiment.remarks, self.remarks)
        self.assertEqual(self.dust_experiment.dust_type, self.dust_type)
        self.assertEqual(len(self.dust_experiment.dust_events), 1)
        self.assertEqual(self.dust_experiment.dust_events[0].timestamp, 1L)


    def tearDown(self):
        '''Closes the HDF5 file that was opened in memory.'''
        self.hdf5_file.close()


    def test_open_hdf5_file(self):
        '''Tests dust_events_hdf5.open_hdf5_file.'''
        f = dust_events_hdf5.open_hdf5_file('test.hdf5', 'test version')
        try:
            self.assertTrue(os.path.exists('test.hdf5'))
            self.assertEqual(f['/'].attrs['file version'], 'test version')
            self.assertEqual(len(f['/'].attrs['when created']), 19)
        finally:
            f.close()
            os.remove('test.hdf5')


    def test_create_user_group(self):
        '''Tests dust_events_hdf5.create_user_group.'''
        user_grp = dust_events_hdf5.create_user_group(self.hdf5_file, self.user_name)
        self.assertTrue(self.user_name in user_grp.parent.keys())


    def test_to_numpy_records(self):
        '''Tests dust_events_hdf5.to_numpy_records.'''
        records = dust_events_hdf5.to_numpy_records([self.dust_event])
        self.assertEqual(len(records), 1)
       

    def test_add_experiments_to_user_group(self):
        '''Tests dust_events_hdf5.add_experiments_to_user_group.'''
        user_grp = dust_events_hdf5.create_user_group(self.hdf5_file, self.user_name)
        dust_events_hdf5.add_experiments_to_user_group(user_grp, [self.dust_experiment])
        ds = user_grp[self.experiment_name]
        self.assertTrue(ds is not None)
        self.assertEqual(ds.attrs['remarks'], self.remarks)
        self.assertEqual(ds.attrs['chemical comp'], self.dust_type.chemical_comp)
        self.assertEqual(ds.attrs['batch'], self.dust_type.batch)
        for key in self.event_dict:
            self.assertEqual(ds[key][0], self.event_dict[key])


    def test_create_dust_events_dataset(self):
        '''Tests dust_events_hdf5.create_dust_events_dataset.'''
        config = {'attribute name': 'attribute value'}
        dust_events_hdf5.create_dust_events_dataset(self.hdf5_file,
               config, [self.dust_event])
        self.assertTrue(self.hdf5_file['/Dust events'].attrs['attribute name'] 
                  == 'attribute value')
        


if __name__ == "__main__":
    unittest.main()
